package com.ruoyi.car.service.impl;

import com.ruoyi.car.domain.CarGoods;
import com.ruoyi.car.domain.CarType;
import com.ruoyi.car.mapper.CarGoodsMapper;
import com.ruoyi.car.service.ICarGoodsService;
import com.ruoyi.car.service.ICarOrderService;
import com.ruoyi.car.service.ICarTestDriveService;
import com.ruoyi.car.service.ICarTypeService;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 车辆管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-07
 */
@Service
public class CarGoodsServiceImpl implements ICarGoodsService {
    @Autowired
    private CarGoodsMapper carGoodsMapper;

    @Autowired
    private ICarTypeService carTypeService;

    @Autowired
    @Lazy
    private ICarOrderService carOrderService;

    @Autowired
    @Lazy
    private ICarTestDriveService carTestDriveService;

    /**
     * 查询车辆管理
     *
     * @param id 车辆管理主键
     * @return 车辆管理
     */
    @Override
    public CarGoods selectCarGoodsById(Long id) {
        return carGoodsMapper.selectCarGoodsById(id);
    }

    /**
     * 查询车辆管理列表
     *
     * @param carGoods 车辆管理
     * @return 车辆管理
     */
    @Override
    public List<CarGoods> selectCarGoodsList(CarGoods carGoods) {
        List<CarGoods> carGoodsList = carGoodsMapper.selectCarGoodsList(carGoods);
        this.fillCardGoods(carGoodsList);
        return carGoodsList;
    }

    private void fillCardGoods(List<CarGoods> carGoodsList) {
        if (!CollectionUtils.isNotEmpty(carGoodsList)) {
            return;
        }
        List<CarType> carTypes = carTypeService.selectCarTypeList(new CarType());
        Map<Long, String> carTypeMap = CollectionUtils.isNotEmpty(carTypes)
                ? carTypes.stream().collect(Collectors.toMap(CarType::getId, CarType::getTypeName))
                : new HashMap<>();
        carGoodsList.forEach(carGoods1 -> {
            carGoods1.setTypeName(carTypeMap.get(carGoods1.getTypeId()));
            carGoods1.setIsCarStr(Objects.equals(carGoods1.getIsCar(), true) ? "是" : "否");
        });
    }

    /**
     * 新增车辆管理
     *
     * @param carGoods 车辆管理
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertCarGoods(CarGoods carGoods) {
        carGoods.setCreateTime(DateUtils.getNowDate());
        int i = carGoodsMapper.insertCarGoods(carGoods);
        afterSave(carGoods);
        return i;
    }

    /**
     * 修改车辆管理
     *
     * @param carGoods 车辆管理
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateCarGoods(CarGoods carGoods) {
        carGoods.setUpdateTime(DateUtils.getNowDate());
        int i = carGoodsMapper.updateCarGoods(carGoods);
        afterSave(carGoods);
        return i;
    }

    public void afterSave(CarGoods carGoods) {
        //当保存成功后，更新其他表的冗余字段
        String goodsName = carGoods.getGoodsName();
        Long goodsId = carGoods.getId();
        carOrderService.updateCarOrderGoodsName(goodsName, goodsId);
        carTestDriveService.updateCarTestDriveGoodsName(goodsName, goodsId);
    }

    /**
     * 批量删除车辆管理
     *
     * @param ids 需要删除的车辆管理主键
     * @return 结果
     */
    @Override
    public int deleteCarGoodsByIds(Long[] ids) {
        return carGoodsMapper.deleteCarGoodsByIds(ids);
    }

    /**
     * 删除车辆管理信息
     *
     * @param id 车辆管理主键
     * @return 结果
     */
    @Override
    public int deleteCarGoodsById(Long id) {
        return carGoodsMapper.deleteCarGoodsById(id);
    }

    @Override
    public int countAll() {
        return carGoodsMapper.countAll();
    }
}
